package cn.tlb.gl.demo1;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import cn.tlb.xutilsdownload.R;

public class GLDemoActivity extends AppCompatActivity {
    private static final String TAG = "GLDemoActivity";
    private GLSurfaceView glSurface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gldemo);
        glSurface = (GLSurfaceView) findViewById(R.id.glSurface);
        glSurface.setRenderer(new GLSurfaceView.Renderer() {
            Square square = new Square();
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                Log.i(TAG, String.format("onSurfaceCreated(GL10 %s, EGLConfig %s)", gl, config));
                gl.glDisable(GL10.GL_DITHER);
                gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
                gl.glClearColor(0, 0, 0, 0);
                gl.glShadeModel(GL10.GL_SMOOTH);
                gl.glEnable(GL10.GL_DEPTH_TEST);
                gl.glDepthFunc(GL10.GL_LEQUAL);
            }

            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
                Log.i(TAG, String.format("onSurfaceChanged(GL10 %s, int %d, int %d)", gl, width, height));
                gl.glViewport(0, 0, width, height);
                gl.glMatrixMode(GL10.GL_PROJECTION);
                gl.glLoadIdentity();
                gl.glFrustumf(-2, 2, -2, 2, 1, 10);
            }

            @Override
            public void onDrawFrame(GL10 gl) {
               //; Log.i(TAG, String.format("onDrawFrame(GL10 %s)", gl));
                // Clears the screen and depth buffer.
                gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
                        GL10.GL_DEPTH_BUFFER_BIT);
                gl.glLoadIdentity();
                gl.glTranslatef(0, 0, -4);
                // Draw our square.
                square.draw(gl); // ( NEW )
            }
        });
    }
}
